<template>
  <div class="login-page">
    <el-card class="login-card">
      <template #header>
        后台管理系统
        <div @click="$router.push('/reg')">去注册</div>
      </template>
      <el-form ref="loginRef" :model="form" :rules="rules">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入用户密码"></el-input>
        </el-form-item>
        <el-form-item class="tc">
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getUserInfoAPI, loginAPI } from '@/api'
import { mapMutations } from 'vuex'

export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        username: 'Zc20220921',
        password: '222222'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10的字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapMutations(['updateToken', 'updateUserInfo']),
    async login () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        // 1. 发起登录的请求
        const { data: res } = await loginAPI(this.form)
        // 2. 登录失败
        if (res.code !== 0) return this.$message.error('用户名或密码错误')
        // 3. 登录成功
        this.$message.success(res.message)
        // 4. 保存到vuex中
        this.updateToken(res.token)
        // 获取最新用户信息
        const { data: res2 } = await getUserInfoAPI()
        console.log(res2)
        if (res2.code === 0) this.updateUserInfo(res2.data)
        // 登录成功之后，跳转到后台主页
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/images/login_bg.jpg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-card {
    width: 540px;
    height: 464px;

    ::v-deep .el-card__header {
      padding-top: 48px;
      height: 80px;
      line-height: 40px;
      height: 140px;
      font-size: 44px;
      text-align: center;
      color: #333;
      border-bottom: none;

      div {
        margin-top: 8px;
        display: block;
        text-decoration-line: none;
        font-size: 16px;
        color: #1E5EFF;
      }
    }
  }

  .el-form {
    margin: 0 auto;
    width: 420px;
    text-align: center;

    .tc {
      text-align: center;
      height: 48px;

      .el-button {
        width: 420px;
      }
    }
  }
}

</style>
